<?php

namespace plugin\ycMassage\app\model;

use Exception;
use plugin\ycMassage\utils\enum\ActionIdentity;
use plugin\ycMassage\utils\enum\State;
use think\facade\Db;

class MassageUserSalesman extends Model
{
    public static function commission($MassageUserSalesman,$MassageUserChannels, $options)
    {
        $salesmanConfig = getHpConfig('', $MassageUserSalesman->saas_appid, 'salesman');
        $commission=(int)$salesmanConfig['commission'];
        if($MassageUserSalesman->commission){
            $commission=(int)$MassageUserSalesman->commission;
        }
        if($commission>0){
            $channels_money = round($options['commission'] * $commission / 100, 4);
        }else{
            $channels_money=0;
        }
        $platform_money=0;
        $platform_commission=(int)$salesmanConfig['platform_commission'];
        if($MassageUserSalesman->platform_commission){
            $platform_commission=(int)$MassageUserSalesman->platform_commission;
        }
        $platform_money=round($options['price']*$platform_commission/100,4);
        if($platform_money<0){
            $platform_money=0;
        }
        $commission=$channels_money+$platform_money;
        if($commission<0){
            return [0,0];
        }
        $remarks = "来自{$MassageUserChannels->title}订单号：{$options['trade']}";
        if ($options['transaction_freeze_period']) {
            $MassageUserWallet = MassageUserWallet::where(['uid' => $MassageUserSalesman->uid])->lock(true)->find();
            if (!$MassageUserWallet) {
                throw new Exception('用户钱包不存在');
            }
            $MassageUserFreezeMoney = new MassageUserFreezeMoney();
            $MassageUserFreezeMoney->uid = $MassageUserSalesman->uid;
            $MassageUserFreezeMoney->form_uid = $MassageUserChannels->uid;
            $MassageUserFreezeMoney->saas_appid = $MassageUserSalesman->saas_appid;
            $MassageUserFreezeMoney->action = ActionIdentity::SALESMAN['value'];
            $MassageUserFreezeMoney->money = $commission;
            $MassageUserFreezeMoney->orders_id = $options['orders_id'];
            $MassageUserFreezeMoney->state = State::NO['value'];
            $MassageUserFreezeMoney->unfreezing_time = date('Y-m-d H:i:s', strtotime("+{$options['transaction_freeze_period']} hours"));
            $MassageUserFreezeMoney->remarks = $remarks;
            $MassageUserFreezeMoney->save();
            $MassageUserWallet->salesman_freeze_money = Db::raw('salesman_freeze_money+' . $commission);
            $MassageUserWallet->save();
        } else {
            MassageUserSalesmanBill::increase($remarks, $MassageUserSalesman->uid, $commission, $MassageUserChannels->id);
        }
        $MassageUserSalesmanStatistics = MassageUserSalesmanStatistics::where(['saas_appid' => $MassageUserSalesman->saas_appid, 'uid' => $MassageUserSalesman->uid])->whereDay('create_time')->find();
        if (!$MassageUserSalesmanStatistics) {
            $MassageUserSalesmanStatistics = new MassageUserSalesmanStatistics;
            $MassageUserSalesmanStatistics->uid = $MassageUserSalesman->uid;
            $MassageUserSalesmanStatistics->saas_appid = $MassageUserSalesman->saas_appid;
        }
        foreach ($options['statistics'] as $key => $value) {
            if ($value) {
                $MassageUserSalesmanStatistics->{$key} = Db::raw("{$key}+{$value}");
            }
        }
        $MassageUserSalesmanStatistics->commission = Db::raw("commission+{$commission}");
        $MassageUserSalesmanStatistics->save();
        return [$platform_money,$channels_money];
    }
}
